package org.belkodevelop.userdatails.adapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.belkodevelop.role.domain.Role;
import org.belkodevelop.user.domain.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;

public class UserDetailsAdapter extends org.springframework.security.core.userdetails.User {
   
    private final long id;
    
    private final User user;
    
    public UserDetailsAdapter(User userEntity) {
        super(userEntity.getEmail(), userEntity.getPassword(), userEntity.isEnabled(), true, true, true, toAuthorities(userEntity.getRoles()));
        this.id = userEntity.getId();
        this.user = userEntity;
    }

    private static Collection<GrantedAuthority> toAuthorities(List<Role> authorities) {
        Collection<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>();
        for (Role authority: authorities) {
            authorityList.add(new GrantedAuthorityImpl(authority.getName()));
        }
        return authorityList;
    }
    
    public long getId() {
        return id;
    }
    
    public User getUser() {
        return user;
    }

}